package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.oms.inbound_order.*;
import io.github.talelin.latticy.service.oms.InboundOrderService;
import io.github.talelin.latticy.vo.*;
import io.github.talelin.latticy.vo.oms.InboundOrderDetailsVo;
import io.github.talelin.latticy.vo.oms.InboundOrderPageVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 入库单表前端控制器
 *
 * @author generator@mrxiao
 * @since 2022-05-16
 */
@RestController
@RequestMapping("/v1/inboundOrder")
@PermissionModule(value = "入库单管理")
public class InboundOrderController {

    @Resource
    private InboundOrderService inboundOrderService;

    @PostMapping("/createInboundOrder")
    @LoginRequired
    @PermissionMeta(value = "创建入库单", mount = false)
    public CreatedVO create(@RequestBody @Validated InboundOrderCreateDTO insertVo) {
        inboundOrderService.createInboundOrder(insertVo);
        return new CreatedVO();
    }

    @PostMapping("/updateInboundOrder")
    @LoginRequired
    @PermissionMeta(value = "更新入库单", mount = false)
    public UpdatedVO update(@RequestBody @Validated InboundOrderUpdateDTO updateVo) {
        inboundOrderService.updateInboundOrder(updateVo);
        return new UpdatedVO();
    }

    @PostMapping("/redo")
    public boolean redo(@RequestBody @Validated List<ReceivingRecordVO> receivingRecordVOS) {
       return inboundOrderService.redoInboundOrder(receivingRecordVOS);
    }

    @PostMapping("/draft")
    @LoginRequired
    @PermissionMeta(value = "入库单批量拉入作废状态", mount = false)
    public DeletedVO draft(@RequestBody @Validated InboundOrderListDTO listDTO) {
        inboundOrderService.draftInboundOrder(listDTO);
        return new DeletedVO();
    }

    @PostMapping("/audit")
    @GroupRequired
    @PermissionMeta(value = "入库单批量审核", mount = false)
    public UpdatedVO audit(@RequestBody @Validated InboundOrderListDTO listDTO) {
        inboundOrderService.auditInboundOrder(listDTO);
        return new UpdatedVO();
    }

    @PostMapping("/putAway")
    @GroupRequired
    @PermissionMeta(value = "入库单批量上架", mount = false)
    public UpdatedVO putAway(@RequestBody @Validated InboundOrderOpenFeignDTO inboundOrderDTO) {
        inboundOrderService.putAwayInboundOrder(inboundOrderDTO);
        return new UpdatedVO();
    }

    @PostMapping("/deleteInboundOrder")
    @GroupRequired
    @PermissionMeta(value = "删除入库单", mount = false)
    public DeletedVO delete(@RequestBody @Validated InboundOrderListDTO listDTO) {
        inboundOrderService.deleteInboundOrder(listDTO.getIdList());
        return new DeletedVO();
    }


    @GetMapping("/receiving/{inboundOrderNo}")
    @GroupRequired
    @PermissionMeta(value = "根据入库单号查询入库单", mount = false)
    public InboundOrderDetailsVo getInboundOrderByNo(@PathVariable(value = "inboundOrderNo") String inboundOrderNo) {
        return inboundOrderService.getInboundOrderByNo(inboundOrderNo);
    }

    @GetMapping("/page")
    @GroupRequired
    @PermissionMeta(value = "查询入库单列表")
    public PageResponseVO<InboundOrderPageVo> page(@Validated InboundOrderPageDTO dto) {
        IPage<InboundOrderPageVo> iPage = inboundOrderService.getInboundOrderPage(dto);
        return PageUtil.build(iPage);
    }


    @PostMapping("/receiving")
    @GroupRequired
    @PermissionMeta(value = "入库单收货", mount = false)
    public UpdatedVO inboundOrderReceiving(@RequestBody @Validated ReceivingRecordDTO receivingRecordDTO) {
        inboundOrderService.inboundOrderReceiving(receivingRecordDTO);
        return new UpdatedVO();
    }



}
